package cn.openea.perpartner.meily.web.entity.dto;

import cn.hutool.json.JSONUtil;
import cn.openea.perpartner.common.core.exception.ServiceException;
import cn.openea.perpartner.meily.constant.enums.ExpressCompany;
import cn.openea.perpartner.meily.constant.enums.ExpressStatus;
import cn.openea.perpartner.meily.web.entity.LogisticsInfo;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Function;

/**
 * @author ysk
 * @version 1.0.1
 * @Description 物流信息DTO
 * @createTime 2023年05月10日 13:31:00
 */
@Component
public class LogisticsDTOFactory {

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @SuperBuilder
    public static class LogisticsInfoDTO {
        private String id;
        /**
         * 模块id
         */
        private String moduleId;
        /**
         * 所属业务模块
         */
        private String module;
        /**
         * 用户id
         */
        private String userId;
        /**
         * 货物名称
         */
        private String goodsName;
        /**
         * 交易时间
         */
        private LocalDateTime tradingTime;
        /**
         * 发货时间
         */
        private LocalDateTime deliveryTime;
        /**
         * 延迟收货天数
         */
        private Integer delayedReceiptDays;
        /**
         * 交易编号，如果是团购商品就是交易编号，测评就是主文档id
         */
        private String tradingId;
        /**
         * 手机号
         */
        private String phone;
        /**
         * 物流单号
         */
        private String courierNumber;
        /**
         * 快递公司对应
         */
        private String expressCompany;
        /**
         * 快递公司名称
         */
        private String expressCompanyName;
        /**
         * 收货人地址
         */
        private String consigneeAddress;
        /**
         * 收货人名称
         */
        private String consigneeName;
        /**
         * 物流状态，0 揽收,1 在途,2 派件,3 签收，99 暂无物流
         */
        private Short status;
        /**
         * 物流状态 对应名称
         */
        private String statusName;
        /**
         * 对应业务状态，0 未完成 1 已完成
         */
        private Short businessStatus;
        /**
         * 物流流向
         */
        private List<LogisticsSingleDynamicDTO> logisticsDynamicList;
        /**
         * 自动收货天数
         */
        private Long automaticReceiptDays;
    }

    public Function<LogisticsInfo, LogisticsInfoDTO> toInfoDTO = (logisticsInfo) -> {
        if (logisticsInfo == null) {
            return null;
        }
        LogisticsInfoDTO logisticsInfoDTO = new LogisticsInfoDTO();
        BeanUtils.copyProperties(logisticsInfo, logisticsInfoDTO);
        String dynamicContext = logisticsInfo.getDynamicContext();
        if (StringUtils.isNotBlank(dynamicContext) && !JSONUtil.isJson(dynamicContext)) {
            throw new ServiceException("数据异常");
        }
        // 根据发货时间计算自动收货天数
        LocalDateTime deliveryTime = logisticsInfo.getDeliveryTime();
        LocalDateTime receivingTime = deliveryTime.plusDays(10 + logisticsInfo.getDelayedReceiptDays());
        long daysDifference = Duration.between(LocalDateTime.now(), receivingTime).toDays();
        if (daysDifference < 0 || logisticsInfo.getBusinessStatus() == (short) 1) {
            // 已经自动收货完成了
            daysDifference = 0L;
        }
        logisticsInfoDTO.setAutomaticReceiptDays(daysDifference);
        ExpressCompany expressCompany = ExpressCompany.getByCode(logisticsInfo.getExpressCompany());
        ExpressStatus expressStatus = ExpressStatus.getInstance(logisticsInfo.getStatus());
        logisticsInfoDTO.setExpressCompanyName(expressCompany != null ? expressCompany.getName() : logisticsInfo.getExpressCompany());
        logisticsInfoDTO.setStatusName(expressStatus != null ? expressStatus.getStatusName() : "状态异常");
        List<LogisticsSingleDynamicDTO> dynamicDTOList = JSONObject.parseArray(dynamicContext, LogisticsSingleDynamicDTO.class);
        logisticsInfoDTO.setLogisticsDynamicList(dynamicDTOList);
        return logisticsInfoDTO;
    };

    /**
     * 实时物流数据
     */
    @Data
    public static class LogisticsRealTimeDTO {

        private Short state;
        /**
         * 快递公司编码
         */
        private String com;

        private List<LogisticsSingleDynamicDTO> logisticsDynamicList;

    }

    /**
     * 物流单个网点信息
     */
    @Data
    public static class LogisticsSingleDynamicDTO {
        /**
         * 到达网点时间 格式化后的
         */
        private LocalDateTime ftime;
        /**
         * 行政区域的名称
         */
        private String areaName;
        /**
         * 内容
         */
        private String context;
        /**
         * 物流状态
         */
        private String status;
    }

}
